package com.likou;

import org.w3c.dom.Node;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * Author: 陈桂明
 * Date: 2025/4/16 15:56
 * Description: q155
 */
public class q155 {
    public static void main(String[] args) {
        q155 minStack = new q155();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        minStack.getMin();  // --> 返回 -3.
        minStack.pop();
        minStack.top();     // --> 返回 0.
        minStack.getMin();
    }

    // Deque<Integer> stack;
    static class Node {
        Integer num;
        Node nextNode;

        public Node() {
        }

        public Node(int val) {
            this.num = val;
        }

        public Node(int val, Node node) {
            this.num = val;
            this.nextNode = node;
        }
    }

    static class HeardNode {
        int min = Integer.MAX_VALUE;
        int len;
        Node p;

        public HeardNode() {
        }

        public HeardNode(int len) {
            this.len = len;
        }

        public HeardNode(int len, Node node) {
            this.len = len;
            this.p = node;
        }
    }

    HeardNode stack;

    public q155() {
        stack = new HeardNode(0, null);
    }

    public void push(int val) {
        Node node = new Node(val, null);
        node.nextNode = stack.p;
        stack.p = node;
        stack.len++;
        // 更新最小值
        stack.min = Math.min(stack.min, val);
    }

    public void pop() {
        Node q = stack.p;
        stack.p = stack.p.nextNode;
        stack.len--;
        // 更新最小值
        if (stack.min == q.num) {
            stack.min = queryMin();
        }
        q = null;
    }

    public int top() {
        return stack.p.num;
    }

    public int getMin() {
        return stack.min;
    }

    public int queryMin() {
        int min = Integer.MAX_VALUE;
        Node q = stack.p;
        while (q != null) {
            min = Math.min(min, q.num);
            q = q.nextNode;
        }

        return min;
    }
}
